<?php

$s="123计算机";
echo strlen($s), "\n";
echo mb_strlen($s), "\n";
//strcmp用于比较两个字符串的大小
var_dump(strcmp(string1:"Hello", string2: "Hi"));
var_dump(strcmp(string1:"Hi",string2:"hi"));
var_dump(strcmp(string1:"Hi~", string2:"Hi"));
var_dump(strcasecmp(string1:"Hi",string2: "hi"));
//trim、ltrim、rtrim删除字符串两侧，左侧、右侧的空白
$s="tt How are you?  \t   ";
echo"[$s]\n";
echo "[", trim($s), "]\n";
echo"[", ltrim($s), "]\n";
echo "[", rtrim($s), "]\n";
echo "[", trim(",.;'Hello___","_.,';"),"]\n";
//stppos函数返回在字符串haystack中needle首次出现的从开始的位置
$s="How old are you?";
echo strpos($s, needle:"o"),"\n";
echo strrpos($s,"o"), "\n";
$pos=0;
echo $pos=strpos($s, "o",$pos),"\n";
echo $pos=strpos($s, "o",++$pos),"\n";
echo $pos=strpos($s, "o",++$pos),"\n";
var_dump($pos=strpos($s,"o",++$pos));
//bin2hex将字符串的每个字节转换为两个16进制位
$a='计算机123';
echo bin2hex($a),"\n";
echo hex2bin('e8aeale7ae97e69cba313233'), "\n";
//chunk split把字符串用指定的字符分隔开来
echo chunk_split( "123456",2,":"), "\n";
echo chunk_split( "e8aea1e7ae97e69cba313233", 2,""), "\n";
echo chunk_split( "e8aeale7ae97e69cba313233", 2,"_"),"\n";
//str_replace字符串替换
$s="How old are you?";
echo str_replace( 'o', '**', $s),"\n";
//从字符串中，不管找到o还是r，统统替换成*
echo str_replace(['o','r'],'*',$s),"\n";
//字符串中，找到o替换成0，找到r替换成R
echo str_replace(['o', 'r'], ['O', 'R'], $s), "\n";
//两个字符串，都按上面的规则处理
print_r(str_replace(['o', 'r'], ['O','R'], [$s, 'Hero']));
//  Array
//  (
//        [0]=>HOw Old aRe yOu?
//        [1]=>HeRo
//  )
echo str_replace( '','',$s),"\n";
//str_repeat多次重复字符串
//strtolower、strtoupper转换成小写、大写
echo strtolower("How Are You.\n"); //how are you.
echo strtoupper("I'm fine, Thank you.\n"); //I'M FINE, THANK YOU.
echo strtoupper("e8_ae_a1_e7_ae_97\n"); //E8_AE_A1_E7_AE_97
//printf显示格式化字符串
$a=32.856;
printf("[%f] [%1$.1f] [%1$6.1f] [%1$-6.1f]\n", $a); //[32.856000] [32.9] [  32.9] [32.9  ]
$s="Hello World!";
printf("[%s] [%1$.5s] [%1$8.5s] [%1$'-8.5s]\n", $s); //[Hello World!] [Hello] [   Hello] [---Hello]
$s="汉字";
printf("[%s] [%1$.3s] [%1$6.3s] [%1$'-8.3s]\n", $s); //[汉字] [汉] [   汉] [-----汉]
//substr取子串
echo substr("Hello",0,3),"\n"; //Hel
echo substr("Hello",2),"\n"; //llo
//-3表示字符串后边的3个字符不要了
echo substr("Hello",0,-3),"\n"; //He
//-4表示从倒数第4个字符开始取子串
echo substr("Hello",-4,-2),"\n"; //el
//str_starts_with、str_ends_with 以子串开始或结束
var_dump(str_starts_with("Hello","ll")); //bool(false)
var_dump(str_starts_with("Hello","he")); //bool(false)
var_dump(str_starts_with("Hello","He")); //bool(true)
var_dump(str_ends_with("Hello","llo")); //bool(true)